package com.show.task;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.show.business.device.bean.DeviceManage;
import com.show.business.device.bean.vo.AlarmTmpVo;
import com.show.business.device.service.IDeviceAlarmService;
import com.show.business.device.service.IDeviceManageService;
import com.show.business.device.service.IUartTmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 设备离线定时任务处理
 * @author ccx
 */
@Slf4j
@Component("deviceOfflineTask")
public class DeviceOfflineTask {

    @Resource
    private IDeviceAlarmService deviceAlarmService;

    @Resource
    private IDeviceManageService deviceManageService;

    @Resource
    private IUartTmpService uartTmpService;
    /**
     * 设备离线定时任务（在第 0 秒 触发。每 3分钟 执行一次）
     */
    @Transactional(rollbackFor = Exception.class)
    public void syscDeviceOffline() {
        log.info("deviceOfflineTask_syscDeviceOffline开始执行，时间：{}", DateUtil.now());
        List<DeviceManage> deviceList=deviceManageService.getList(null);//获取所有的设备信息
        if(deviceList!=null && deviceList.size()>0){
            deviceList.stream().forEach(k -> {
                AlarmTmpVo alarm = uartTmpService.getAlarmByImei(k.getImei());
                if (ObjectUtil.isNotEmpty(alarm)) {
                    //修改设备状态、发短信提醒
                    deviceAlarmService.updateDeviceAlarm(alarm);
                }
            });
        }
        log.info("deviceOfflineTask_syscDeviceOffline结束执行，时间：{}", DateUtil.now());
    }

}
